#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8410
#define BUFFER_SIZE 1024
#define WWW_DIR "./www/"

void handle_client(int client_socket)
{
    char buffer[BUFFER_SIZE];
    memset(buffer, 0, BUFFER_SIZE);

    // 读取请求
    ssize_t bytes_read = read(client_socket, buffer, sizeof(buffer) - 1);
    if (bytes_read < 0)
    {
        perror("Failed to read request");
        close(client_socket);
        return;
    }

    printf("Received request: %s ", buffer);

    // 从请求中获取文件名
    char method[16], path[BUFFER_SIZE];
    sscanf(buffer, "%s %s", method, path);

    // 检查请求方法是否为 GET
    if (strcmp(method, "GET") != 0)
    {
        const char *http_response = "HTTP/1.1 405 Method Not Allowed\r"
                                    "Connection: close\r"
                                    "\r";
        write(client_socket, http_response, strlen(http_response));
        close(client_socket);
        return;
    }

    // 构建完整的文件路径
    char file_path[BUFFER_SIZE];
    snprintf(file_path, sizeof(file_path), "%s%s", WWW_DIR, path + 1); // 跳过第一个 '/'

    // 打开请求的文件
    FILE *file = fopen(file_path, "rb");
    if (file == NULL)
    {
        const char *http_response = "HTTP/1.1 404 Not Found\r"
                                    "Connection: close\r"
                                    "\r"
                                    "<html><body><h1>404 Not Found</h1></body></html>";
        write(client_socket, http_response, strlen(http_response));
        close(client_socket);
        return;
    }

    // 读取文件内容并发送给客户端
    const char *http_response_header = "HTTP/1.1 200 OK\r"
                                       "Content-Type: text/html\r"
                                       "Connection: close\r"
                                       "\r";
    write(client_socket, http_response_header, strlen(http_response_header));

    // 逐块读取文件并发送
    char file_buffer[BUFFER_SIZE];
    size_t nread;
    while ((nread = fread(file_buffer, 1, sizeof(file_buffer), file)) > 0)
    {
        write(client_socket, file_buffer, nread);
    }

    fclose(file);
    close(client_socket);
}

int main()
{
    int server_socket, client_socket;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_len = sizeof(client_addr);

    // 创建 socket
    server_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (server_socket < 0)
    {
        perror("Failed to create socket");
        return 1;
    }

    // 配置服务器地址结构
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY; // 监听所有 IP 地址
    server_addr.sin_port = htons(PORT);       // 使用指定的端口

    // 绑定 socket
    if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        perror("Failed to bind");
        close(server_socket);
        return 1;
    }

    // 监听连接
    if (listen(server_socket, 5) < 0)
    {
        perror("Failed to listen");
        close(server_socket);
        return 1;
    }

    printf("HTTP server listening on port %d...  ", PORT);
    fflush(stdout); // 新增这行

    while (1)
    {
        // 接受客户端连接
        client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_len);
        if (client_socket < 0)
        {
            perror("Failed to accept connection");
            continue; // 继续下一次循环处理其他连接
        }

        // 处理客户端请求
        handle_client(client_socket);
    }

    // 关闭服务器 socket
    close(server_socket);
    return 0;
}